package util

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/spf13/cast"
	"log"
	"net/http"
	"strings"
)

func GetDataToHtml() {
	var (
		url = "https://www.bochk.com/whk/rates/exchangeRatesUSD/exchangeRatesUSD-input.action?lang=hk"
		//orm          gdb.DB
		err error
		//reptileMoney *model.FxReptileMoney
	)

	// 发送GET请求获取网页内容
	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	// 检查请求是否成功
	if resp.StatusCode != 200 {
		log.Fatalf("无法获取网页内容，状态码：%d", resp.StatusCode)
	}

	// 使用goquery解析HTML页面
	doc, err := goquery.NewDocumentFromReader(resp.Body)
	if err != nil {
		log.Fatal(err)
	}
	doc.Find("#form-div > form > div > table.form_table.import-data.second-right > tbody").Each(func(i int, selection *goquery.Selection) {
		for j := 3; j < selection.Find("tr").Length(); j = j + 2 {
			name := strings.TrimSpace(selection.Find("tr:nth-child(" + cast.ToString(j) + ") > td:nth-child(1)").Text())
			sell := strings.TrimSpace(selection.Find("tr:nth-child(" + cast.ToString(j) + ") > td:nth-child(2)").Text())
			buy := strings.TrimSpace(selection.Find("tr:nth-child(" + cast.ToString(j) + ") > td:nth-child(3)").Text())
			fmt.Println(name)
			fmt.Println(sell)
			fmt.Println(buy)
		}
		//fmt.Println(sell)
		//fmt.Println(buy)
		//orm, err = db.GetDBInstance()
		//if err != nil {
		//	panic("连接数据库异常")
		//}
		//if name != "" && sell != "" && buy != "" {
		//	if err = orm.Model("fx_reptile_money").Where("sell=? and buy =?", sell, buy).
		//		Scan(&reptileMoney); err != nil {
		//		panic("数据查询异常")
		//	}
		//	if reptileMoney == nil {
		//		if _, err = orm.Model("fx_reptile_money").Insert(&model.FxReptileMoney{
		//			Name: strings.TrimSpace(name),
		//			Buy:  strings.TrimSpace(buy),
		//			Sell: strings.TrimSpace(sell),
		//		}); err != nil {
		//			panic("插入数据异常")
		//		}
		//	}
		//}
	})
}
